package com.yuyue.dronesystem.controllers;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * ActionController
 */
@RequestMapping(path = "/action")
@Controller
public class ActionController {

    @PostMapping(value = "/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        RedirectAttributes redirectAttributes,
                        HttpSession session) {
        System.out.println(123);
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            redirectAttributes.addFlashAttribute("hello", "登录成功！");
            session.setAttribute("loginUser", username);
            // 登录成功，防止表单重复提交，可重定向
            return "redirect:/main";
        } else {
            redirectAttributes.addFlashAttribute("msg", "用户名密码错误！");
            return "redirect:/login";
        }
    }
}